package com.ysdz.tibetan.tibetanmini.wechat.controller;

import com.ysdz.tibetan.tibetanmini.wechat.exception.AesException;
import com.ysdz.tibetan.tibetanmini.wechat.service.WeChatService;
import com.ysdz.tibetan.tibetanmini.wechat.utils.WXPublicUtils;
import com.ysdz.tibetan.tibetanminicommons.common.ReturnMsg;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class WeChatController {

    @Autowired
    private WeChatService weChatService;

    // 获取 openid
    @PostMapping("/login/auth")
    public ReturnMsg getWxUserOpenid(@RequestBody String code, String appId, String appSecret) {
        return weChatService.getWxUserOpenid(code, appId, appSecret);
    }

    @RequestMapping("/wxpublic/verifyWxToken")
    public String verifyWXToken(HttpServletRequest request) throws AesException {
        String msgSignature = request.getParameter("signature");
        String msgTimestamp = request.getParameter("timestamp");
        String msgNonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
            return echostr;
        }
        return null;
    }

}
